<template>
	<view class="content">
		<u--form labelPosition="left" labelWidth="70" :model="userInfo" :rules="rules" ref="form1">
			<u-form-item label="来源名称" prop="name">
				<u--input
				    v-model="userInfo.name" placeholder="请输入名称"
				    border="surround"
				  ></u--input>
			</u-form-item>
		</u--form>
		<u-button @click="submit" color="#19BC9C">添加</u-button>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				show: false,
				userInfo: {
					name: '',
					busid: null,
				},
				rules: {
					'name': {
						type: 'string',
						required: true,
						message: '请输入来源名称',
						trigger: ['blur', 'change']
					},
				},
				LoginUser: {},
			}
		},
		onReady() {
			//如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则。
			this.$refs.form1.setRules(this.rules)
			this.LoginUser = uni.getStorageSync('LoginUser') ? uni.getStorageSync('LoginUser') : {};
			this.$u.auth.check()
		},
		methods: {
			submit(){
				this.$refs.form1.validate().then(async(res) => {
					let data = {
						name: this.userInfo.name
					}
					let result = await this.$u.api.business.sourceAdd(data)
					if(result.code == 1){
						this.$refs.uToast.show({
							type: 'success',
							message: result.msg,
							complete() {
								uni.navigateTo({
									url: '/pages/business/source/index'
								});
							}
						})
					}  else {
						this.$refs.uToast.show({
							type: 'error',
							message: result.msg
						})
					}
				}).catch(errors => {})
				
			}
		}
	}
</script>

<style>
	.content{
		width: 90%;
		height: 100vh;
		margin: 0 auto;
	}
</style>
